package net.zdreamer.geracomium.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.geracomium.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import net.zdreamer.geracomium.adapter.GeracomiumIntroViewPagerAdapter;
import net.zdreamer.geracomium.adapter.ReserveAdapter;
import net.zdreamer.geracomium.entity.GeracomiumReserveInfos;
import net.zdreamer.geracomium.myviews.CustomViewPager;
import net.zdreamer.geracomium.utils.window.ViewControlor;

import java.util.ArrayList;
import java.util.List;

public class GeracomiumReserveFragment extends Fragment implements OnPageChangeListener {

    private PullToRefreshListView pullToRefreshListView;
    private ReserveAdapter mAdapter;

    int index = 0;
    List<View> advPics = new ArrayList<View>();
    List<ImageView> list_vs;// 点
    private LinearLayout DotsOnViewPager;
    private CustomViewPager customViewPager;
    private GeracomiumIntroViewPagerAdapter viewPagerAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_reserve, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
    }

    private void initViews() {
        pullToRefreshListView = (PullToRefreshListView) getActivity().findViewById(R.id.listview_reserve);
        pullToRefreshListView.setMode(Mode.PULL_FROM_START);
        pullToRefreshListView.setOnRefreshListener(orl);
        pullToRefreshListView.getRefreshableView().addHeaderView(
                initViewPager(), null, false);
        pullToRefreshListView.getRefreshableView().setAdapter(null);

        GeracomiumReserveInfos infos = new GeracomiumReserveInfos();
        mAdapter = new ReserveAdapter(getActivity(), infos.items, R.layout.details_reserve);
        pullToRefreshListView.setAdapter(mAdapter);
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(3000);
                        Message msg = new Message();
                        msg.what = 0;
                        handler.sendMessage(msg);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        customViewPager.setOnPageChangeListener(this);

    }

    private View getHeaderView() {
        View view = (LinearLayout) View.inflate(this.getActivity(),
                R.layout.geracomium_reserve_header, null);
        LinearLayout layout = (LinearLayout) getActivity().findViewById(R.id.ll_header);
        ImageView headerImage = (ImageView) view.findViewById(R.id.image_header);
        ViewControlor.setSize(getActivity(), headerImage, 12, 5, true);
        ;
        return view;
    }

    /*
     * 初始化ViewPager
     */
    private View initViewPager() {
        final View view = View.inflate(getActivity(),
                R.layout.viewpager_homepage, null);
        list_vs = new ArrayList<ImageView>();
        customViewPager = (CustomViewPager) view
                .findViewById(R.id.viewpager_homepage);
        DotsOnViewPager = (LinearLayout) view.findViewById(R.id.assss);
        viewPagerAdapter = new GeracomiumIntroViewPagerAdapter(this);
        customViewPager.setAdapter(viewPagerAdapter);
        viewPagerAdapter.notifyDataSetChanged();
        ViewControlor.setSize(getActivity(), customViewPager, 12, 5, true);
        int count = viewPagerAdapter.getCount();
        initDots(count, 0, false);
        return view;
    }

    /*
     * 初始化Docs
     */
    private void initDots(int cc, int cur, boolean f) {
        for (int j = 0; j < cc; j++) {
            if (f) {
                if (j == cur) {
                    list_vs.get(j).setImageResource(R.drawable.page_now);
                } else {
                    list_vs.get(j).setImageResource(R.drawable.page);
                }
            } else {
                ImageView v = new ImageView(this.getActivity());
                v.setPadding(3, 0, 3, 0);
                if (j == 0) {
                    v.setImageResource(R.drawable.page_now);
                } else {
                    v.setImageResource(R.drawable.page);
                }
                list_vs.add(v);
                DotsOnViewPager.addView(v);
            }
        }

    }

    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    index++;
                    customViewPager.setCurrentItem(index % list_vs.size());
                    break;

                default:
                    break;
            }
        }

        ;
    };
    private OnRefreshListener<ListView> orl = new OnRefreshListener<ListView>() {

        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            String label = DateUtils.formatDateTime(
                    getActivity().getApplicationContext(),
                    System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);

            refreshView.getLoadingLayoutProxy()
                    .setLastUpdatedLabel("上次刷新：" + label);


            Thread a = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message message = new Message();
                    message.what = 0;
                    h.sendMessage(message);
                }
            });
            a.start();
        }
    };
    Handler h = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                GeracomiumReserveInfos infos = new GeracomiumReserveInfos();
                mAdapter = new ReserveAdapter(getActivity(), infos.items, R.layout.details_reserve);
                pullToRefreshListView.setAdapter(mAdapter);
                pullToRefreshListView.onRefreshComplete();
            }
        }

    };

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int arg0) {
        customViewPager.setCurrentItem(arg0);
        initDots(list_vs.size(), arg0, true);
    }
}

